package com.asiainfo.integration.actions.opentext;

import org.apache.commons.io.FilenameUtils;
import org.apache.log4j.Logger;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;

/**
 * Usage:
 *
 * @author Charkey
 * @date 2015/12/23
 */
@WebServlet(urlPatterns = "/opentext/download", asyncSupported = true)
public class OpenTextDownloadAction extends HttpServlet {

    public static final Logger INFO_LOG = Logger.getLogger(OpenTextDownloadAction.class);

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store");
        response.setHeader("Content-Type", "image/jpeg");
        response.setHeader("Date", new Date().toString());
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Server", "myserver");
        String contextPath = request.getServletContext().getRealPath("/");
        String docType = request.getParameter("customer_document_type");
        File file;
        if ("POA".equals(docType)) {
            file = new File(contextPath + "/static/POA.jpg");
        } else if ("POI".equals(docType)) {
            file = new File(contextPath + "/static/POI.jpg");
        } else {
            file = new File(contextPath + "/static/POA.jpg");
        }
        FileInputStream inputStream = new FileInputStream(file);
        byte[] bytes = new byte[inputStream.available()];
        int size = inputStream.read(bytes);
        response.getOutputStream().write(bytes);
        response.flushBuffer();
    }

}
